<template>
  <div class="container">
    <div class="is-size-4 ml-3 my-4 has-text-weight-bold">
      编写试卷
      <div class="primary-color is-size-4">——</div>
    </div>

    <el-form label-width="120px">
      <el-form-item label="科目">
        <el-select
          v-model="form.subjectId"
          placeholder="请选择科目"
          @change="
            form.questions = []
            form.score = 0
          "
        >
          <el-option
            v-for="(item, index) in subjectList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="题目选择">
        <el-button size="small" @click="fetchQuestions">选择...</el-button>
        <i
          v-if="form.questions.length !== 0"
          class="el-icon-success has-text-success m-4"
        />
      </el-form-item>

      <el-form-item label="试卷名称">
        <el-input
          v-model="form.name"
          style="width:500px"
          placeholder="请输入内容"
        />
      </el-form-item>

      <el-form-item label="试卷年级">
        <el-input-number
          v-model="form.level"
          style="width:500px"
          :min="1"
          :max="12"
          placeholder="请输入试卷年级"
        />
      </el-form-item>

      <el-form-item label="建议时长(分钟)">
        <el-input-number
          v-model="form.suggestTime"
          :min="10"
          :step="10"
          style="width:500px"
          placeholder="建议时长(分钟)"
        />
      </el-form-item>

      <el-form-item label="全卷满分" class="">
        <span class="is-size-4 primary-color mx-2">{{ form.score }}</span>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>

    <!-- 选择试卷弹窗 -->
    <el-dialog
      width="80%"
      title="选择题目"
      fullscreen
      :visible.sync="questionTableVisible"
    >
      <el-table
        highlight-current-row
        class="ml-4"
        style="width:97%"
        stripe
        :data="questionListData"
        @selection-change="handleQuestionSelection"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="info.desc" label="题干" />
        <el-table-column prop="gradeLevel" label="年级" width="60" />
        <el-table-column
          prop="questionType"
          label="题型"
          :formatter="difficultFormat"
          width="80"
        />
        <el-table-column prop="score" label="分值" width="70" />
        <el-table-column prop="difficult" label="难度" width="80" />
        <el-table-column
          prop="createtime"
          :formatter="dateFormat"
          label="创建日期"
          width="180"
        />
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="questionTableVisible = false"
        >确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const paperApi = require('@/api/paper')
const commonApi = require('@/api/common')
const questionApi = require('@/api/question')
const dayjs = require('dayjs')

export default {
  data() {
    return {
      subjectList: [],
      totalScore: 0,
      questionTableVisible: false,
      questionListData: [],
      form: {
        subjectId: '',
        name: '',
        level: '',
        score: 0,
        suggestTime: '',
        questions: ''
      }
    }
  },
  computed: {},
  mounted() {
    this.fetchSubjcets()
  },
  methods: {
    difficultFormat(row) {
      return row.questionType === 1 ? '单选题' : '多选题'
    },

    dateFormat(row) {
      const day = dayjs(row.createtime)
      return day.format('YYYY-MM-DD HH:mm:ss')
    },

    fetchSubjcets() {
      commonApi.getSubjectList().then(res => {
        console.log('subjcet loaded')
        this.subjectList = res.data
        this.form.subjectId = res.data[4].id
      })
    },

    fetchQuestions() {
      this.questionTableVisible = true
      questionApi.getQuestionListBySubject(this.form.subjectId).then(res => {
        this.questionListData = res.data
      })
    },

    handleQuestionSelection(selArr) {
      this.form.questions = ''
      this.form.score = 0
      selArr.forEach(element => {
        this.form.questions = this.form.questions + element.questionId + ','
        this.form.score += element.score
      })
    },

    onSubmit() {
      const loading = this.$loading()
      if (this.form.questions.length === 0) {
        this.$message('未选择任何题目！')
        loading.close()
        return
      }
      console.log(this.form)
      paperApi
        .addNewPaper(this.form)
        .then(res => {
          loading.close()
          console.log(res)
          this.$confirm('发布成功，跳转到列表查看', '', {
            confirmButtonText: '去看看',
            type: 'success'
          })
            .then(() => {
              this.$router.push('/papers/list')
            })
            .catch(() => {
              this.form = {
                subjectId: '',
                name: '',
                level: '',
                score: 0,
                suggestTime: '',
                questions: ''
              }
            })
        })
        .catch(rej => {
          this.$notify.error({
            title: '失败',
            message: '发生错误'
          })
        })
    }
  }
}
</script>

<style lang="scss">
.container {
  margin-bottom: 100px;
}

.btn {
  font-size: 17px;
  margin-bottom: 15px;
  width: 100%;
  height: 65px;
  text-align: left;
}
</style>
